package org.alino.core.shiro.filter;

import org.alino.core.Consts;
import org.alino.core.shiro.model.UserBase;
import org.alino.core.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @Desc TODO
 * @Author by Brant
 * @Date 2017/1/17
 */
public class CurrentUserFilter extends PathMatchingFilter {
    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        Object principal = SecurityUtils.getSubject().getPrincipal();
        if(StringUtils.isNotBlank(principal)){
            UserBase token = (UserBase) principal;
            request.setAttribute(Consts.APP_CURRENT_USER_KEY,token);
        }
        return true;
    }
}
